﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TravianHelper.app;
using System.Collections;
using System.IO;
using TravianHelper.hero;
using TravianHelper;

namespace TravianHelper.oasis
{
    public partial class OasisPool : Form
    {
        public OasisPool()
        {
            InitializeComponent();
            updateListViewTarget(TravianApplicationContext.oasisPool);
        }

        private void buttonSearchAdd_Click(object sender, EventArgs e)
        {
            int x = Convert.ToInt32(textBoxX.Text);
            int y = Convert.ToInt32(textBoxY.Text);
            for (int i = 0; i < TravianApplicationContext.allOasis.Count; i++)
            {
                Zone zone = (Zone)TravianApplicationContext.allOasis[i];
                zone.distance = Math.Sqrt((zone.x - x) * (zone.x - x) + (zone.y - y) * (zone.y - y));
                int maxDist = Convert.ToInt32(textBoxMaxDist.Text);
                if (zone.distance <= maxDist &&!TravianApplicationContext.oasisPool.ContainsKey(zone.x +","+ zone.y))
                {                    
                    TravianApplicationContext.oasisPool.Add(zone.x +","+ zone.y, new Oasis( zone));
                }
            }
            updateListViewTarget(TravianApplicationContext.oasisPool);
        }

        private void updateListViewTarget(Hashtable zones)
        {
            listViewTarget.Items.Clear();
            ArrayList oasis = new ArrayList (zones.Values);
            oasis.Sort(new OasisSort());

            foreach (Oasis zone in oasis)
            {
                listViewTarget.Items.Add(new ListViewItem(new string[] { zone.x.ToString() + "," + zone.y.ToString(), zone.distance2Nearest.ToString(), zone.status, zone.defend.ToString(), zone.occupied ? "occupied" : "" }));
            }
            totalOasis.Text = "Total oasis/farm: " + oasis.Count;
        }

        private void buttonClear_Click(object sender, EventArgs e)
        {
            TravianApplicationContext.oasisPool.Clear();
            updateListViewTarget(TravianApplicationContext.oasisPool);
        }



        private void buttonShuffle_Click(object sender, EventArgs e)
        {
           // village.oasis = Util.Shuffle(village.oasis);
           // updateListViewTarget(village.oasis);
        }

        private void buttonSort_Click(object sender, EventArgs e)
        {
            updateListViewTarget(TravianApplicationContext.oasisPool);
        }

        delegate void AddListViewCallback(string[] items);

        

        private void AddListView(string[] items)
        {
            // InvokeRequired required compares the thread ID of the
            // calling thread to the thread ID of the creating thread.
            // If these threads are different, it returns true.
            if (this.listViewTarget.InvokeRequired)
            {
                AddListViewCallback d = new AddListViewCallback(AddListView);
                this.Invoke(d, new object[] { items });
            }
            else
            {
                listViewTarget.Items.Add(new ListViewItem(items));
            }
        }

        private void OasisPool_Load(object sender, EventArgs e)
        {

        }
 

    }
}
